package com.cal.utms.dao.maintenance;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.cal.utms.entity.DealType;
import com.cal.utms.util.HibernateUtil;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class DealTypeDao {

	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;
	static SessionFactory factory;

	public Object add(DealType dealType) {
		Object obj;
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		System.out.println("Saving DealType");
		try {
			tx = session.beginTransaction();
			obj = session.save(dealType);
			tx.commit();
			System.out.println("Deal Type saved");
		} catch (Exception ex) {
			tx.rollback();
			obj = new Object();
			System.out.println("Exception in saving DealType " + ex.getMessage());
			ex.printStackTrace();
		} finally {
			session.close();
		}
		return obj;
	}

	public DealType getById(Integer id) {
		Session session = HibernateUtil.getSession().openSession();
		DealType dealType = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			dealType = (DealType) session.get(DealType.class, id);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getByID DealType " + e.getMessage());
		} finally {
			session.close();
		}
		return dealType;
	}

	@SuppressWarnings("unchecked")
	public List<DealType> getDealTypeList() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<DealType> dealTypeList = null;
		try {
			tx = session.beginTransaction();
			dealTypeList = session.createQuery("from DealType").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getDealTypeList " + e.getMessage());
		} finally {
			session.close();
		}
		return dealTypeList;
	}

	public static void main(String[] args) {
		System.out.println("Starting...");
		DealTypeDao dealTypeDao = new DealTypeDao();

		// Saving
		/*
		 * InstrumentType insType = new InstrumentType();
		 * insType.setInstrumentName("TEst Ins1");
		 * insType.setInstrumentType(com.cal.enumtype.InstrumentType.G);
		 * insType.setScripts((byte) 0); insType.setListed((byte) 0);
		 * insType.setDenominals((byte) 0);
		 * insType.setWithholdingTax(WithholdingTax.M);
		 * insType.setInterestRate(InterestRate.F);
		 * insType.setInterestType(InterestType.D); insType.setDayBasis("A");
		 * insType.setCouponAvailability((byte) 0);
		 * insType.setCuponFrequency(0);
		 * 
		 * System.out.println(insType); Object res =
		 * instrumentTypeDao.addInstrumentType(insType);
		 */

		// retrieving all
		List<DealType> list = dealTypeDao.getDealTypeList("purch");
		for (DealType dealType : list) {
			System.out.println(dealType.getId() + " : " + dealType.getDealType());
		}
		System.out.println(dealTypeDao.getDealTypeByCode("sale").getDescription());
		System.out.println("Done ");
	}

	@SuppressWarnings("unchecked")
	public List<DealType> getDealTypeList(String querry) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<DealType> dealTypeList = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from DealType where upper(dealType) like :name");
			query.setParameter("name", querry.toUpperCase() + "%");
			dealTypeList = query.list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getDealTypeList " + e.getMessage());
		} finally {
			session.close();
		}
		return dealTypeList;
	}

	public DealType getDealTypeByCode(String code) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		DealType dealType = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from DealType where upper(code) = :code");
			query.setParameter("code", code.toUpperCase());
			dealType = (DealType) query.list().get(0);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getDealTypeByCode " + e.getMessage());
		} finally {
			session.close();
		}
		return dealType;
	}

	@SuppressWarnings("unchecked")
	public List<DealType> getRollOverDealType() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<DealType> dealTypeList = null;
		try {
			tx = session.beginTransaction();
			dealTypeList = session.createQuery("from DealType D where D.rollover=1").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getDealTypeList " + e.getMessage());
		} finally {
			session.close();
		}
		return dealTypeList;
	}

}
